//
//  NVRLeftView.m
//  TestNVR
//
//  Created by xzh on 14-7-29.
//  Copyright (c) 2014年 xzh. All rights reserved.
//

#import "NVRLeftView.h"
#import "NVRLeftViewCell.h"

@implementation NVRLeftView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        
      
        
    }
    return self;
}

-(id)initWithFrame:(CGRect)frame withChannelArray:(NSArray *)channelArray{
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        self.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width);
        self.backgroundColor = [UIColor clearColor];
     
        
        _channelArray = [[NSArray alloc] initWithArray:channelArray];
        
        _tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 50, [UIScreen mainScreen].bounds.size.width) style:UITableViewStylePlain];
        [_tableview setDelegate:self];
        [_tableview setDataSource:self];
        [_tableview setSeparatorStyle:UITableViewCellSeparatorStyleSingleLine];
        if ([_tableview respondsToSelector:@selector(separatorInset)]) {
            _tableview.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
        }
        [self addSubview:_tableview];
        

        [_tableview reloadData];
        
        //  添加长按手势
        UILongPressGestureRecognizer *longPressReger = [[UILongPressGestureRecognizer alloc]
                                                        
                                                        initWithTarget:self action:@selector(handleLongPress:)];
        
        longPressReger.minimumPressDuration = 1.0;
        
        [_tableview addGestureRecognizer:longPressReger];
        
        
        
        //  添加平移手势
        UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGestureAction:)];
        [_tableview addGestureRecognizer:panRecognizer];
        panRecognizer.delegate = self;
        
    }
    return self;
}



-(void)handleLongPress:(UILongPressGestureRecognizer *)gestureRecognizer

{
    
    
    
    CGPoint point = [gestureRecognizer locationInView:_tableview];
    NSIndexPath *indexPath = [_tableview indexPathForRowAtPoint:point];
    
    if(gestureRecognizer.state == UIGestureRecognizerStateBegan)
        
    {
         NSLog(@"----===%f    %f", point.x,  point.y);
        if (indexPath != nil) {
            
            NVRLeftViewCell *cell = (NVRLeftViewCell *)[_tableview cellForRowAtIndexPath:indexPath];
            self.selectedIndexPath = indexPath;
            cell.alpha = 0.5;
            //  获取当前图片的中心点位置
            [UIView animateWithDuration:0.3 animations:^{
                //  回到原始位置
                
                _moveAbleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(point.x-cell.backImageView.frame.size.width/2, point.y-cell.backImageView.frame.size.height/2, cell.backImageView.frame.size.width, cell.backImageView.frame.size.height)];
                _moveAbleImageView.backgroundColor = [UIColor whiteColor];
                
            } completion:^(BOOL finished) {
                //  动画结束，从父视图移除
                [self addSubview:_moveAbleImageView];
                cell.alpha = 1.0;
            }];

            [gestureRecognizer cancelsTouchesInView];
        }
    }
    
    else if(gestureRecognizer.state == UIGestureRecognizerStateEnded)
        
    {
            NVRLeftViewCell *cell = (NVRLeftViewCell *)[_tableview cellForRowAtIndexPath:self.selectedIndexPath];
            cell.alpha = 0.5;
            //  获取当前图片的中心点位置
            [UIView animateWithDuration:0.3 animations:^{
                //当不允许替换的时候返回到原始位置
                _moveAbleImageView.frame = cell.frame;
            } completion:^(BOOL finished) {
                //  动画结束，从父视图移除
                [_moveAbleImageView removeFromSuperview];
                cell.alpha = 1.0;
            }];
        
        NSLog(@"-------==%f    %f", point.x,  point.y);
        
    }
    
    else if(gestureRecognizer.state == UIGestureRecognizerStateChanged)
        
    {
        

        
    }
    
   
    
    
}




//  平移手势

- (void)panGestureAction:(UIPanGestureRecognizer *)recognizer
{
    if (recognizer.state == UIGestureRecognizerStateBegan) {

        CGPoint point = [recognizer locationInView:self];
        
        _primitiveCenter = point;
        
        _moveAbleImageView.center = _primitiveCenter;
    }

    if (recognizer.state == UIGestureRecognizerStateChanged) {
        
        //  手指偏移量
        CGPoint translation = [recognizer translationInView:self];
        //  随手指移动
        CGPoint p;
        p.x = translation.x+_primitiveCenter.x;
        p.y = translation.y + _primitiveCenter.y;
        _moveAbleImageView.center = p;

    }
   
}



#pragma mark -
#pragma mark -- gesture delegate

//  返回yes，使自定义添加的手势，与原有手势共存，默认情况下 该方法返回NO。NO的情况下，自定义的UIPanGestureRecognizer 与collectionview原有的拖动手势冲突，自定义的将不响应。
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    
    return YES;
}

#pragma mark -
#pragma mark UITableViewDataSource


/**
 @method 定义header高度
 @param tableView 列表View
 @param section 选中列表索引
 @result CGFloat 高度
 */
#pragma mark - header高度 -
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

/**
 @method tableview的个数
 @param tableView 列表View
 @result NSInteger 个数
 */
#pragma mark - 列表个数 -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

//二维数组个数
/**
 @method tableview的个数
 @param tableView 列表View
 @param section 第几项
 @result NSInteger 个数
 */
#pragma mark - 列表个数 -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  _channelArray.count;
}

#pragma mark - 列表cell内容 -
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //通讯录cell
    static NSString *aIdentifier = @"Cell";
    NVRLeftViewCell *aCell = nil;
    
    if (aCell == nil)
    {
        aCell = [[NVRLeftViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:aIdentifier];
        
    }
    
    aCell.label.text = [_channelArray objectAtIndex:indexPath.row];
    return aCell;
}

#pragma mark -
#pragma mark UITableViewDelegate

#pragma mark - 列表高度 -
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"-----  点击 %@", [_channelArray objectAtIndex:indexPath.row]);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
